package com.zzw.mvvm.pages.demo;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zzw.mvvm.R;
import com.zzw.mvvm.base.BaseActivity;
import com.zzw.mvvm.base.recyclebase.CommonAdapter;
import com.zzw.mvvm.base.recyclebase.MultiItemTypeAdapter;
import com.zzw.mvvm.base.recyclebase.base.ViewHolder;
import com.zzw.mvvm.base.recyclebase.slidehelper.RecycleItemTouchHelper;
import com.zzw.mvvm.base.recyclebase.slidehelper.ScrollFlagRecycleView;
import com.zzw.mvvm.beans.DemoEntity;
import com.zzw.mvvm.viewmodels.demo.RecycleDemoViewModel;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class RecycleViewActivity extends BaseActivity<RecycleDemoViewModel> {


    @BindView(R.id.demo_recycler)
    ScrollFlagRecycleView demoRecycler;
    List<DemoEntity> list = new ArrayList<>();
    @BindView(R.id.demo_bt)
    Button demoBt;
    @BindView(R.id.m_smart_refresh)
    SmartRefreshLayout smartRefresh;


    @Override
    public int getLayoutId() {
        return R.layout.activity_recycle_view;
    }

    @OnClick(R.id.demo_bt)
    public void onViewClicked() {
        //viewModel.update();
    }

    @Override
    public void bindViewModel() {
        viewModel.getListDemo().observe(this, new Observer<List<DemoEntity>>() {
            @Override
            public void onChanged(List<DemoEntity> demoEntities) {
                list.clear();
                list.addAll(demoEntities);
                demoRecycler.getAdapter().notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        demoRecycler.setLayoutManager(new LinearLayoutManager(this));
        demoRecycler.setAdapter(new MCommenAdapter(this, R.layout.item_demo_layout, list));
        ((MCommenAdapter) demoRecycler.getAdapter()).setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        //滑动--
        ItemTouchHelper.Callback callback=new RecycleItemTouchHelper((RecycleItemTouchHelper.ItemTouchHelperCallback) demoRecycler.getAdapter());
        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(demoRecycler);
        //上拉下拉
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.update();
                smartRefresh.finishLoadMore();
            }
        });
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.clearList();
                smartRefresh.finishRefresh();
            }
        });

    }



    class MCommenAdapter extends CommonAdapter {
        public MCommenAdapter(Context context, int layoutId, List datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, Object o, int position) {
            holder.setText(R.id.demo_tv, list.get(position).getName());
        }

    }


}
